import {Component, OnInit} from '@angular/core';
import {JsonUtils, ArrayUtils, HighlightUtils} from 'great-jsutils';

@Component({
  selector: 'app-array-test02',
  templateUrl: './array-test02.component.html',
  styleUrls: ['./array-test02.component.less']
})
export class ArrayTest02Component implements OnInit {

  json1 = [{
    'name': '山东省',
    'id': 'id01',
    'children': [
      {
        'name': '济南市',
        'id': 'id01-01'
      }
    ]
  }, {
    'name': '河北省',
    'id': 'id02',
    'children': [
      {
        'name': '沧州市',
        'id': 'id02-01'
      }
    ]
  }];
  json2 = {
    'name': 'text',
    'id': 'nodeId'
  };
  json2Str = null;
  json1HtmlStr = null;

  constructor() {
  }

  ngOnInit() {
    this.json2Str = JSON.stringify(this.json2, undefined, 2);
    this.json1HtmlStr = `<pre>${HighlightUtils.syntaxHighlight(this.json1)}</pre>`;
  }

  updateKey() {
    ArrayUtils.updateKey(this.json1, JSON.parse(this.json2Str));
    this.ngOnInit();
  }

  updateKey1() {
    ArrayUtils.updateKey(this.json1, JSON.parse(this.json2Str), item => {
      if (item['children'] || item['items']) {
        return {'leaf': false};
      } else {
        return {'leaf': true};
      }
    });
    this.ngOnInit();
  }

}
